import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/config/app_config.dart';
import 'package:flutter_douban/config/event_bus_util.dart';
import 'package:flutter_douban/config/sqflite/class_db_provider.dart';
import 'package:flutter_douban/main_page/class/model/class_item_model.dart';
import 'package:flutter_douban/main_page/class/provider/class_dateil_provider.dart';
import 'package:flutter_douban/main_page/class/provider/class_provider_model.dart';
import 'package:flutter_douban/tool_views/app_dialog.dart';
import 'package:like_button/like_button.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ClassDetailBottowView extends StatefulWidget {
  final ClassItemModel model;
  const ClassDetailBottowView({Key? key, required this.model})
      : super(key: key);

  @override
  _ClassDetailBottowViewState createState() => _ClassDetailBottowViewState();
}

class _ClassDetailBottowViewState extends State<ClassDetailBottowView> {
  final int likeCount = 0;

  Future<bool> onLikeButtonTapped(bool isLiked) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? numberId = prefs.getString(AppConfig.numberId);
    if(numberId == null){
      AppDialog.showText(context, title: "请先登录~");
      return widget.model.is_collection!;
    }
    ClassDbProvider provider = new ClassDbProvider();
    if(widget.model.is_collection! == true){
      provider.delete(int.parse(widget.model.id!),numberId);
    }else{
      provider.insert(widget.model,numberId);
    }
    /// 延时更新数据
    Future.delayed(const Duration(milliseconds: 500), () {
      EventBusUtil.fire(CacheClassEvent(CacheClassEvent.cacheClassIn));
    });
    return !widget.model.is_collection!;
    
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.only(left: 0, right: 10),
      height: 60,
      alignment: Alignment.center,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Container(
            width: MediaQuery.of(context).size.width / 3 / 2 + 15,
            height: 40,
            color: Colors.white,
            child: LikeButton(
              size: 20,
              likeCount: likeCount,
              isLiked: widget.model.is_collection,
              countBuilder: (int? count, bool isLiked, String text) {
                Widget result;
                if (isLiked == false) {
                  result = Text(
                    '收藏',
                    style: TextStyle(color: Colors.black87, fontWeight: FontWeight.bold),
                  );
                } else
                  result = Text(
                    '已收藏',
                    style: TextStyle(
                        color: Colors.black87,),
                  );
                return result;
              },
              likeBuilder: (bool isLiked) {
                return Image.asset(
                    isLiked == true
                        ? 'assets/images/common/collcetionicon_on.png'
                        : 'assets/images/common/icon_home_collection.png',
                    width: 20,
                    height: 20);
              },
              likeCountAnimationType: likeCount < 1
                  ? LikeCountAnimationType.part
                  : LikeCountAnimationType.none,
              likeCountPadding: const EdgeInsets.only(left: 5.0),
              onTap: onLikeButtonTapped,
            ),
          ),

          InkWell(
            child: Container(
              alignment: Alignment.center,
              width: MediaQuery.of(context).size.width / 3 - 25,
              height: 35,
              decoration: BoxDecoration(
                  color: AppColors.yellow,
                  borderRadius: BorderRadius.circular(20)),
              child: Text(
                '试看',
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 14,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
          InkWell(
            onTap: () {
              Provider.of<ClassProviderModel>(context, listen: false)
                  .add(widget.model);
              setState(() {
                if (widget.model.is_buy == null) {
                  widget.model.is_buy = false;
                }
                if (widget.model.is_buy == true) {
                  return;
                }
                widget.model.is_buy = !widget.model.is_buy!;
              });
            },
            child: Container(
              alignment: Alignment.center,
              width: MediaQuery.of(context).size.width / 3,
              height: 35,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20),
                  color: (widget.model.is_buy == false ||
                          widget.model.is_buy == null)
                      ? AppColors.yellow
                      : Color.fromRGBO(209, 209, 209, 1)),
              child: Text(
                '购买课程',
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 14,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
